package com.intgrt.manager.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.midentity.PswInfo;
import com.intgrt.manager.service.SysUserService;
import com.intgrt.manager.util.ProjectConstants;

@Controller
public class SysUserCtrl {

	@Autowired
	private SysUserService service;
	private static final String MENU_ID = "7";
	
	@RequestMapping(value = "/sysuser", method = { RequestMethod.GET })
	public String index(HttpServletRequest request) {
		SysUser curuser = (SysUser) request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		String btnids = "";
		try {
			btnids = curuser.getMenuButtons().getString(MENU_ID);
		} catch (Exception e) {
			btnids = "";
		}
		JSONArray btnlist = service.getMenuBtns(curuser.getId(), MENU_ID, btnids);
		request.setAttribute("btnlist", btnlist);
		return "sysuser";
	}
	
	@ResponseBody
	@RequestMapping(value="/d/selfinfo" , method={RequestMethod.POST})
	public String getSelf(HttpServletRequest request){
		SysUser curuser = (SysUser) request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		JSONObject obj=new JSONObject();
		obj.put("accname", curuser.getAccname());
		obj.put("accphone", curuser.getAccphone());
		obj.put("accemail", curuser.getAccemail());
		return obj.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping(value="/d/updself" , method={RequestMethod.POST})
	public String updSelfInfo(HttpServletRequest request, SysUser user) {
		SysUser curuser = (SysUser) request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		user.setId(curuser.getId());
		int res = service.updSelf(user);
		if(0==res){
			curuser.setAccemail(user.getAccemail());
			curuser.setAccname(user.getAccname());
			curuser.setAccphone(user.getAccphone());
			request.getSession().setAttribute(ProjectConstants.SESSION_USER, curuser);
			return ProjectConstants.SUCCESS_CODE;
		}
		return ProjectConstants.FAIL_CODE;
	}
	
	@ResponseBody
	@RequestMapping(value="/d/updpsw" , method={RequestMethod.POST})
	public String updPsw(HttpServletRequest request, PswInfo info) {
		SysUser curuser = (SysUser) request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		return service.updPsw(curuser.getId(), info);
	}
	
	@ResponseBody
	@RequestMapping(value="/d/userlist" , method={RequestMethod.POST})
	public String list(HttpServletRequest request, SysUser user){
		SysUser curuser = (SysUser) request.getSession().getAttribute(ProjectConstants.SESSION_USER);
		return service.getList(user,curuser.getOrgid());
	}
	
	@ResponseBody
	@RequestMapping(value="/d/onlyUserCode" , method={RequestMethod.POST})
	public String onlyCode(HttpServletRequest request){
		String usrcode = request.getParameter("userCode");
		return service.onlyCode(usrcode);
	}
	
	@ResponseBody
	@RequestMapping(value="/d/usrsave" , method={RequestMethod.POST})
	public String usrSave(HttpServletRequest request, SysUser entity){
//		entity.setGrpid(String.valueOf(UserInfo.getGrpid()));
		return service.saveEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/usredit" , method={RequestMethod.POST})
	public String usrEdit(HttpServletRequest request, SysUser entity){
		return service.editEntity(entity);
	}
	@ResponseBody
	@RequestMapping(value="/d/usrdel" , method={RequestMethod.POST})
	public String usrDel(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return service.delEntity(ids);
	}
	@RequestMapping(value = "/logout" , method = {RequestMethod.GET})
	public String logout(HttpServletRequest request){
		SecurityUtils.getSubject().logout();
		request.getSession().removeAttribute(ProjectConstants.SESSION_USER);
		return "redirect:/";
	}
	
}
